package cn.com.header.core.helper;

import java.util.Random;

/**
 * 随机数生成器
 * @author liangzhongqiu789@sina.com
 * @date 2017-03-23
 * @time 10:21
 */
public class RandomGenerator {

    private static Random random = new Random();

    /**
     * 随机数，由字母和数字组成
     * @param length 返回随机数长度
     * @return
     */
    public static String getRandomStringByLength(int length){
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        return getRandomByLength(length, base);
    }


    public static String getRandomByLength(int length, String giveStrs) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(giveStrs.length());
            sb.append(giveStrs.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 纯数字随机数，由数字组成
     * @param length
     * @return
     */
    public static String getRandomNumberByLength(int length){
        String base = "0123456789";
        return getRandomByLength(length, base);
    }

}
